const { defineConfig } = require('@vue/cli-service')
const path = require('path')

function resolve(dir) {
    return path.join(__dirname, dir)
}
module.exports = defineConfig({
    //设置为空打包后不分更目录还是多级目录
    publicPath: '',
    //build编译后存放静态文件的目录
    //assetsDir: "static",

    // build编译后不生成资源MAP文件
    productionSourceMap: false,

    lintOnSave: false,
    devServer: {
        // 关闭eslint语法验证
        overlay: {
            warning: false,
            errors: false
        }
    },
    //开发服务,build后的生产模式还需nginx代理
    devServer: {
        allowedHosts: 'all',
        open: false, //运行后自动打开浏览器
        port: process.env.VUE_APP_PORT, //挂载端口
        proxy: {
            '/api': {
                target: process.env.VUE_APP_API_BASEURL,
                ws: true,
                pathRewrite: {
                    '^/api': '/'
                }
            }
        }
    },
    chainWebpack: config => {
        // 移除 prefetch 插件
        config.plugins.delete('preload');
        config.plugins.delete('prefetch');
        config.resolve.alias.set('vue-i18n', 'vue-i18n/dist/vue-i18n.cjs.js');
        // 原svg规则覆盖了所有的svg图标，需要先将自己的svg排除，以应用新的sprite规则
        // src/assets/icons是我们将要存放svg的目录
        config.module
            .rule('svg')
            .exclude.add(path.join(__dirname, 'src/assets/icons/flowable')) // 排除自定义svg目录
            .end()
        config.module
            .rule('icons') // 新规则
            .test(/\.svg$/)
            .include.add(path.join(__dirname, 'src/assets/icons/flowable')) // 新规则应用于我们存放svg的目录
            .end()
            .use('svg-sprite-loader') // 用sprite-loader接卸
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[dir]-[name]',
            })
            .end()
    },
    configureWebpack: {
        //性能提示
        performance: {
            hints: false
        },
        optimization: {
            splitChunks: {
                chunks: "all",
                automaticNameDelimiter: '~',
                name: "scuiChunks",
                cacheGroups: {
                    //第三方库抽离
                    vendor: {
                        name: "modules",
                        test: /[\\/]node_modules[\\/]/,
                        priority: -10
                    },
                    elicons: {
                        name: "elicons",
                        test: /[\\/]node_modules[\\/]@element-plus[\\/]icons-vue[\\/]/
                    },
                    tinymce: {
                        name: "tinymce",
                        test: /[\\/]node_modules[\\/]tinymce[\\/]/
                    },
                    echarts: {
                        name: "echarts",
                        test: /[\\/]node_modules[\\/]echarts[\\/]/
                    },
                    xgplayer: {
                        name: "xgplayer",
                        test: /[\\/]node_modules[\\/]xgplayer.*[\\/]/
                    },
                    codemirror: {
                        name: "codemirror",
                        test: /[\\/]node_modules[\\/]codemirror[\\/]/
                    }
                }
            }
        }
    }
})
